package com.fourth.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fourth.dao.UBOrderDao;
import com.fourth.service.UBOrderService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljx.entity.Goods;
import com.ljx.entity.Order;

import com.ljx.security.util.WebUtils;
import com.ljx.utils.JwtUtil;
import com.ljx.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @Author：lmy
 * @Date：2023/3/3 15:11
 */
@Service
public class UBOrderServiceImpl extends ServiceImpl<UBOrderDao, Order> implements UBOrderService {

    @Autowired(required = false)
    private UBOrderDao ubOrderDao;

    @Override
    public Result findOrder(Integer currentPage, Integer pageSize, Integer state,Integer commentstatus) {
        String token = WebUtils.getRequest().getHeader("token");
        Map<String, Object> map = JwtUtil.getTokenChaim(token);
        String tel = (String) map.get("username");
        Page<Order> page = new Page<>(currentPage,pageSize);
        Page<Order> orderList = ubOrderDao.findAll(page,tel,state,commentstatus);
        for(Order o : orderList.getRecords()){
            String code = o.getCode();
            List<Goods> goodsList = ubOrderDao.findGoodsBYCode(code,commentstatus);
            BigDecimal sum = BigDecimal.valueOf(goodsList.stream().mapToInt(Goods::getTotalmoney).sum());
            o.setTotalmoney(sum);
            o.setGoodsList(goodsList);
        }
        if (orderList != null){
            return new Result(2000,"查询成功",orderList);
        }
        return new Result(5000,"查询失败");
    }
}
